package com.offcn.member.controller;

import com.offcn.common.utils.JWTUtil;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;
import com.offcn.member.entity.MemberEntity;
import com.offcn.member.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * 会员-会员表
 *
 * @author yangguanlong
 * @email 8107626@qq.com
 * @date 2021-12-03 17:19:02
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private StringRedisTemplate redisTemplate;
//    @Autowired
//    private RedisTemplate redisTemplate;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     *  根据开始时间，结束时间，统计每日注册账号
     */
    @RequestMapping("/countAccountCreate")
    public R countAccountCreate(String beginTime,String endTime){
        List<Map<String, Object>> mapList =
                memberService.countByDateTime(beginTime, endTime);
        System.out.println(mapList);
        return R.ok().put("mapList",mapList);
    }
    /**
     * 校验
     */
    @RequestMapping("/login")
    public R login(String username,String password){
    //判断是否登入
        MemberEntity memberEntity = memberService.login(username, password);
        if (memberEntity==null){
            return R.error("登入失败,账号或密码错误!");
        }
        //登入生成令牌
        String jwt = JWTUtil.createJWT(username);

        //存入缓存 redis
        String key = UUID.randomUUID().toString().replace("-", "");

        redisTemplate.opsForHash().put(key,"jwt",jwt);
        redisTemplate.opsForHash().put(key,"username",username);
        //设置redis过期时间
        redisTemplate.expire(key,JWTUtil.TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
        return R.ok().put("jwt",jwt).put("refresh",key);
    }
    @RequestMapping("/refreshToken")
    public R refreshToken(String refreshToken){
        //通过key值在缓存中获取用户信息
        String username = (String) redisTemplate.boundHashOps(refreshToken).get("username");

        if(StringUtils.isNotEmpty(username)){
            //如果有用户信息，则重新生成令牌
            String jwt = JWTUtil.createJWT(username);
            //将新的令牌放入缓存
            redisTemplate.opsForHash().put(refreshToken,"jwt",jwt);
            redisTemplate.opsForHash().put(refreshToken,"username",username);
            return R.ok().put("jwt",jwt).put("refreshToken",refreshToken);
        }else{
            return R.error("该令牌以失效");
        }
    }
}
